package com.itjiange.codesandbox.impl;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.dfa.FoundWord;
import cn.hutool.dfa.WordTree;
import com.itjiange.codesandbox.CodeSandBox;
import com.itjiange.codesandbox.JavaCodeSandBoxTemplate;
import com.itjiange.model.ExecuteCodeRequest;
import com.itjiange.model.ExecuteCodeResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

/**
 * @author ItJianGe
 * @data 2024/3/26 9:24
 */
@Slf4j
@Component
public class JavaNativeCodeSandBox extends JavaCodeSandBoxTemplate {
    public static final List<String> BLACK_CODE_LIST = Arrays.asList("File", "exec");
    public static final WordTree WORD_TREE;

    static {
        WORD_TREE = new WordTree();
        WORD_TREE.addWords(BLACK_CODE_LIST);
    }

    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest codeRequest) {
        return super.executeCode(codeRequest);
    }

    @Override
    public File getUserCodeFile(String userCode) {
        FoundWord foundWord = WORD_TREE.matchWord(userCode);
        if (foundWord != null) {
            log.error("包含禁用代码！！！");
            return null;
        }
        return super.getUserCodeFile(userCode);
    }
}
